package com.one.controller;

import com.one.annotation.RequestPermission;
import com.one.annotation.RequestUser;
import com.one.bean.Cart;
import com.one.bean.Product;
import com.one.constant.RequestReturnConstant;
import com.one.service.CartService;
import com.one.service.ProductService;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import com.one.vo.ProductParamVo;
import com.one.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Create on 2022-01-19
 * <p>
 * Author: LiangWei
 * <p>
 * Description: 购物车模块
 **/
@RestController
public class CartController {
    @Autowired(required = false)
    private CartService cartService;
    @Autowired(required = false)
    private ProductService productService;
    @PostMapping("/addProductToCart")
    @RequestPermission
    public RequestReturn<Product> addProductToCart(@RequestUser UserVo userVo, @RequestBody ProductParamVo productParamVo){
        if(cartService.addProductToCart(userVo.getUserId(),productParamVo)>0) {
            return RequestReturnFactory.success(RequestReturnConstant.RETURN_SUCCESS_CODE, RequestReturnConstant.ADD_CART_SUCCESS_MSG, productService.selectProductByProdId(productParamVo.getProdId()));
        }
        return RequestReturnFactory.fail(RequestReturnConstant.ADD_CART_FAIL_CODE,RequestReturnConstant.ADD_CART_FAIL_MSG,null);
    }


    @DeleteMapping("/deleteCart")
    @RequestPermission
    public RequestReturn<String> deleteCart(@RequestUser UserVo userVo,@RequestParam Integer cartId){
        if(cartService.deleteProductFromCart(userVo.getUserId(),cartId)>0){
            return RequestReturnFactory.success(RequestReturnConstant.RETURN_SUCCESS_CODE,RequestReturnConstant.DELETE_CART_SUCCESS_MSG,null);
        }
        return RequestReturnFactory.fail(RequestReturnConstant.REMOVE_CART_FAIL_CODE,RequestReturnConstant.REMOVE_CART_FAIL_MSG,null);
    }

    @DeleteMapping("/deleteManyCarts")
    @RequestPermission
    public RequestReturn<String> deleteManyCarts(@RequestParam List<Integer> cartIdList){
        if(cartService.deleteManyProductsFromCart(cartIdList)>0){
            return RequestReturnFactory.success(RequestReturnConstant.RETURN_SUCCESS_CODE,RequestReturnConstant.DELETE_CART_SUCCESS_MSG,null);
        }
        return RequestReturnFactory.fail(RequestReturnConstant.REMOVE_CART_FAIL_CODE,RequestReturnConstant.REMOVE_CART_FAIL_MSG,null);
    }

    @GetMapping("/selectCartsByUserId")
    @RequestPermission
    public RequestReturn<List<Cart>> selectCartsByUserId(@RequestUser UserVo userVo){
        return RequestReturnFactory.success(RequestReturnConstant.RETURN_SUCCESS_CODE,RequestReturnConstant.SELECT_ALL_CART_INFO_SUCCESS_MSG,cartService.selectAllCartsByUserId(userVo.getUserId()));
    }

    @DeleteMapping("/deleteAllCartsByUserId")
    @RequestPermission
    public RequestReturn<String> deleteAllCartsByUserId(@RequestUser UserVo userVo){
        if(cartService.deleteAllCartsByUserId(userVo.getUserId())>0){
            return RequestReturnFactory.success(RequestReturnConstant.RETURN_SUCCESS_CODE,RequestReturnConstant.DELETE_ALL_CART_SUCCESS_MSG,null);
        }
        return RequestReturnFactory.fail(RequestReturnConstant.EMPTY_CART_FAIL_CODE,RequestReturnConstant.EMPTY_CART_FAIL_MSG,null);
    }

    @PostMapping("/updateProductNum")
    @RequestPermission
    public RequestReturn<Cart> updateProductNum(@RequestParam Integer productNum,@RequestParam Integer cartId){
        if(cartService.updateProductNumByCartId(productNum,cartId)>0){
            return RequestReturnFactory.success(RequestReturnConstant.RETURN_SUCCESS_CODE,RequestReturnConstant.UPDATE_CART_PRODUCT_NUM_SUCCESS_MSG,cartService.selectOneCartByCartId(cartId));
        }
        return RequestReturnFactory.fail(RequestReturnConstant.UPDATE_CART_PRODUCTNUM_FAIL_CODE,RequestReturnConstant.UPDATE_CART_PRODUCTNUM_FAIL_MSG,null);
    }

    @PostMapping("/addToCollect")
    @RequestPermission
    public RequestReturn<String> addToCollect(@RequestUser UserVo userVo,@RequestParam Integer productId){
        if(cartService.moveToCollectByUserIdAndProductId(userVo.getUserId(),productId)>0){
            return RequestReturnFactory.success(RequestReturnConstant.MOVE_TO_COLLECT_SUCCESS_CODE,RequestReturnConstant.MOVE_TO_COLLECT_SUCCESS_MSG,null);
        }
        return RequestReturnFactory.fail();
    }
}
